package com.weyoung.app.controller;

import com.weyoung.app.service.RelationService;
import com.weyoung.common.consatant.result.Result;
import com.weyoung.common.model.dto.app.*;
import com.weyoung.common.model.entity.FamilyRelation;
import com.weyoung.common.model.vo.app.RelationNodeDetailVO;
import com.weyoung.common.model.vo.app.RelationTreeVO;
import com.weyoung.common.model.vo.app.UserDetailVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "族谱关系管理")
@RestController
@RequestMapping("/api/relation")
@RequiredArgsConstructor
@Validated
public class RelationController {

    private final RelationService relationService;

    /**
     * 获取家族中未建立族谱关系的成员
     * @param familyId 家族ID
     * @return 未建立族谱关系的成员列表
     */
    @GetMapping("/unrelated-members/{familyId}")
    @Operation(summary = "获取家族中未建立族谱关系的成员")
    public Result<List<UserDetailVO>> getUnrelatedFamilyMembers(@PathVariable("familyId") String familyId) {
        return Result.success(relationService.getUnrelatedFamilyMembers(familyId));
    }

    /**
     * 获取族谱树 -- 默认父系
     * @return 族谱树列表
     */
    @GetMapping("/patrilineal-tree")
    @Operation(summary = "获取父系族谱树")
    public Result<List<RelationTreeVO>> getRelationTree() {
        return Result.success(relationService.getRelationTree());
    }

    /**
     * 获取母系族谱树
     * @return 母系族谱树列表
     */
    @GetMapping("/maternal-tree")
    @Operation(summary = "获取母系族谱树")
    public Result<List<RelationTreeVO>> getMaternalRelationTree() {
        return Result.success(relationService.getMaternalRelationTree());
    }

    /**
     * 创建族谱节点
     * @param dto 族谱节点创建参数
     * @return 创建的族谱节点
     */
    @PostMapping("/node")
    @Operation(summary = "创建族谱节点")
    public Result<FamilyRelation> createRelationNode(@Validated @RequestBody RelationNodeDTO dto) {
        return Result.success(relationService.createRelationNode(dto));
    }

    /**
     * 更新族谱节点
     * @param dto 族谱节点更新参数
     * @return 操作结果
     */
    @PutMapping("/node")
    @Operation(summary = "更新族谱节点")
    public Result<Void> updateRelationNode(@Validated @RequestBody UpdateRelationNodeDTO dto) {
        relationService.updateRelationNode(dto);
        return Result.success();
    }

    /**
     * 递归删除族谱节点及其所有子节点
     * @param relationId 关系ID
     * @return 操作结果
     */
    @DeleteMapping("/node/{relationId}")
    @Operation(summary = "递归删除族谱节点及其所有子节点")
    public Result<Void> deleteRelationNode(@PathVariable("relationId") Long relationId) {
        relationService.deleteRelationNodeRecursive(relationId);
        return Result.success();
    }

    /**
     * 设置配偶关系
     * @param relationId 关系ID
     * @param spouseId 配偶用户ID
     * @return 操作结果
     */
    @Operation(summary = "设置配偶关系")
    @PutMapping("/node/{relationId}/spouse")
    public Result<Void> setSpouse(
            @PathVariable Long relationId,
            @RequestParam Integer spouseId) {
        relationService.setSpouse(relationId, spouseId);
        return Result.success();
    }

    /**
     * 解除配偶关系
     * @param relationId 关系ID
     * @return 更新后的族谱节点
     */
    @Operation(summary = "解除配偶关系")
    @DeleteMapping("/node/{relationId}/spouse")
    public Result<FamilyRelation> removeSpouse(@PathVariable Long relationId) {
        relationService.removeSpouse(relationId);
        return Result.success();
    }

    /**
     * 获取指定世代的所有节点
     * @param dto 世代查询参数
     * @return 指定世代的族谱节点列表
     */
    @PostMapping("/generation")
    @Operation(summary = "获取指定世代的所有节点")
    public Result<List<FamilyRelation>> getNodesByGeneration(@Validated @RequestBody GenerationQueryDTO dto) {
        return Result.success(relationService.getNodesByGeneration(dto));
    }

    @GetMapping("/node/{userId}")
    @Operation(summary = "获取族谱节点详细信息")
    public Result<RelationNodeDetailVO> getRelationNodeDetail(@PathVariable("userId") Integer userId) {
        return Result.success(relationService.getRelationNodeDetail(userId));
    }

    @PutMapping("/node/detail")
    @Operation(summary = "编辑族谱节点详细信息")
    public Result<Void> updateRelationNodeDetail(@Validated @RequestBody UpdateRelationNodeDetailDTO dto) {
        relationService.updateRelationNodeDetail(dto);
        return Result.success();
    }

}